package com.sense.kukoo.common.wiget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sense.kukoo.common.R;
import com.sense.kukoo.common.constant.ARouterUrl;
import com.sense.kukoo.common.repo.Repo;
import com.sense.kukoo.common.utils.ApplicationContext;

public class BottomLoginDialog extends Dialog {

    private View root;
    private TextView tv_btn_login;

    private static final long mDuration = 300;

    public BottomLoginDialog(@NonNull Context context) {
        super(context);
    }

    public BottomLoginDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected BottomLoginDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getContext().setTheme(R.style.bottom_dialog);
        setContentView(R.layout.common_bottom_login_dialog);
        //
        root = findViewById(R.id.root);
        tv_btn_login = findViewById(R.id.tv_btn_login);
        //
        root.setOnClickListener(mListener);
        tv_btn_login.setOnClickListener(mListener);
        findViewById(R.id.iv_btn_close).setOnClickListener(mListener);
    }

    @Override
    public void show() {
        super.show();
        root.setTranslationY(ApplicationContext.dp2px(250));
        root.animate().translationY(0).setDuration(mDuration).start();
    }

    @Override
    public void dismiss() {
        root.animate()
                .translationY(ApplicationContext.dp2px(250))
                .setDuration(mDuration)
                .withEndAction(new Runnable() {
                    @Override
                    public void run() {
                        _dismiss();
                    }
                })
                .start();
    }

    private void _dismiss() {
        super.dismiss();
    }

    private final View.OnClickListener mListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.root) {
                dismiss();
            } else if (id == R.id.tv_btn_login) {
                if (bottomLoginListener != null) {
                    bottomLoginListener.onClickLogin();
                }
            } else if(id == R.id.iv_btn_close){
                dismiss();
            }
        }
    };

    public interface BottomLoginListener {
        void onClickLogin();
    }

    private BottomLoginListener bottomLoginListener;

    public BottomLoginDialog setListener(BottomLoginListener l) {
        bottomLoginListener = l;
        return this;
    }

    public static boolean autoCheckAndLogin(Context context){
        if (Repo.INSTANCE.getLogin().isLogin()) {
            return true;
        }
        autoShow(context);
        return false;
    }

    public static BottomLoginDialog autoShow(Context context){
        BottomLoginDialog dialog = new BottomLoginDialog(context);
        dialog.setListener(new BottomLoginListener() {
            @Override
            public void onClickLogin() {
                dialog.dismiss();
                ARouter.getInstance()
                        .build(ARouterUrl.ROUTE_ME_MODULE_LOGIN_ACTIVITY)
                        .navigation();
            }
        });
        dialog.show();
        return dialog;
    }
}
